package com.shj.service.impl;

import com.shj.dao.SysPersonMapper;
import com.shj.entity.SysPerson;
import com.shj.service.SysFamilyService;
import com.shj.service.SysPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysPersonServiceImpl implements SysPersonService {
    @Autowired
    private SysPersonMapper sysPersonMapper;
    @Autowired
    private SysFamilyService sysFamilyService;

    @Override
    public List<SysPerson> findAll(String personName) {
        return sysPersonMapper.findAll(personName);
    }

    @Override
    public List<SysPerson> findHolder(String personName) {
        return sysPersonMapper.findHolder(personName);
    }

    @Override
    public List<SysPerson> findNotPart(String famiCode) {
        return sysPersonMapper.findNotPart(famiCode);
    }

    @Override
    public SysPerson findByPerNo(String perCode) {
        return sysPersonMapper.selectByPrimaryKey(perCode);
    }

    @Override
    public SysPerson findByCardId(String cardId) {
        return sysPersonMapper.findByCardId(cardId);
    }


    /**
     * 添加户主
     * @param familyNo
     * @param sysPerson
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(String familyNo, SysPerson sysPerson) {
        sysPerson.setFamiCode(familyNo);
        sysPerson.setRelation("1");
        sysPerson.setAge((short) 0);
        sysPersonMapper.insert(sysPerson);
    }

    /**
     * 添加成员
     * @param sysPerson
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMember(SysPerson sysPerson) {
        String perNo = creatPerNo(sysPerson.getFamiCode());
        sysPerson.setPersCode(perNo);
        //添加成员
        sysPersonMapper.insert(sysPerson);
        // 家庭人数自增
        int agriNum = sysPerson.getIsRural()?1:0;
        sysFamilyService.countNum(sysPerson.getFamiCode(), agriNum);
    }

    @Override
    public void edit(SysPerson sysPerson) {
        sysPersonMapper.updateByPrimaryKey(sysPerson);
    }

    @Override
    public void addPart(String[] ids) {
        for (String personCode : ids) {
            sysPersonMapper.addPart(personCode);
        }
    }

    @Override
    public void delete(String perCode) {
        sysPersonMapper.deleteByPrimaryKey(perCode);
    }

    /**
     * 自动生成成员编号
     * @param familyNo
     * @return
     */
    @Override
    public String creatPerNo(String familyNo) {
        String maxCode = sysPersonMapper.getMaxCode(familyNo);
        int number=1;
        if(maxCode!=null){
            int beginIndex=maxCode.length()-2;
            String no=maxCode.substring(beginIndex);
            number=Integer.parseInt(no);
            ++number;
            //使用0补足位数
            no=String.format("%02d", number);
            maxCode=familyNo+no;
        }else{
            String no=String.format("%02d", number);
            maxCode=familyNo+no;
        }
        return maxCode;
    }


}
